home *** CD-ROM | disk | FTP | other *** search
/ Freelog 125 / Freelog_MarsAvril2015_No125.iso / Musique / Quod Libet / quodlibet-3.3.0-installer.exe / bin / test / script_helper.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2014-12-31  |  6KB  |  164 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.7)
  3.  
  4. import sys
  5. import os
  6. import re
  7. import os.path as os
  8. import tempfile
  9. import subprocess
  10. import py_compile
  11. import contextlib
  12. import shutil
  13.  
  14. try:
  15.     import zipfile
  16. except ImportError:
  17.     pass
  18.  
  19. from test.test_support import strip_python_stderr
  20.  
  21. def _assert_python(expected_success, *args, **env_vars):
  22.     cmd_line = [
  23.         sys.executable]
  24.     if not env_vars:
  25.         cmd_line.append('-E')
  26.     cmd_line.extend(args)
  27.     env = os.environ.copy()
  28.     env.update(env_vars)
  29.     p = subprocess.Popen(cmd_line, stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE, env = env)
  30.     
  31.     try:
  32.         (out, err) = p.communicate()
  33.     finally:
  34.         subprocess._cleanup()
  35.         p.stdout.close()
  36.         p.stderr.close()
  37.  
  38.     rc = p.returncode
  39.     err = strip_python_stderr(err)
  40.     if (rc or expected_success or not rc) and not expected_success:
  41.         raise AssertionError('Process return code is %d, stderr follows:\n%s' % (rc, err.decode('ascii', 'ignore')))
  42.     return (rc, out, err)
  43.  
  44.  
  45. def assert_python_ok(*args, **env_vars):
  46.     '''
  47.     Assert that running the interpreter with `args` and optional environment
  48.     variables `env_vars` is ok and return a (return code, stdout, stderr) tuple.
  49.     '''
  50.     return _assert_python(True, *args, **env_vars)
  51.  
  52.  
  53. def assert_python_failure(*args, **env_vars):
  54.     '''
  55.     Assert that running the interpreter with `args` and optional environment
  56.     variables `env_vars` fails and return a (return code, stdout, stderr) tuple.
  57.     '''
  58.     return _assert_python(False, *args, **env_vars)
  59.  
  60.  
  61. def python_exit_code(*args):
  62.     cmd_line = [
  63.         sys.executable,
  64.         '-E']
  65.     cmd_line.extend(args)
  66.     with open(os.devnull, 'w') as devnull:
  67.         return subprocess.call(cmd_line, stdout = devnull, stderr = subprocess.STDOUT)
  68.  
  69.  
  70. def spawn_python(*args, **kwargs):
  71.     cmd_line = [
  72.         sys.executable,
  73.         '-E']
  74.     cmd_line.extend(args)
  75.     return subprocess.Popen(cmd_line, stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.STDOUT, **kwargs)
  76.  
  77.  
  78. def kill_python(p):
  79.     p.stdin.close()
  80.     data = p.stdout.read()
  81.     p.stdout.close()
  82.     p.wait()
  83.     subprocess._cleanup()
  84.     return data
  85.  
  86.  
  87. def run_python(*args, **kwargs):
  88.     p = spawn_python(*args, **kwargs)
  89.     stdout_data = kill_python(p)
  90.     return (p.wait(), stdout_data)
  91.  
  92.  
  93. def temp_dir():
  94.     dirname = tempfile.mkdtemp()
  95.     dirname = os.path.realpath(dirname)
  96.     
  97.     try:
  98.         yield dirname
  99.     finally:
  100.         shutil.rmtree(dirname)
  101.  
  102.  
  103. temp_dir = contextlib.contextmanager(temp_dir)
  104.  
  105. def make_script(script_dir, script_basename, source):
  106.     script_filename = script_basename + os.extsep + 'py'
  107.     script_name = os.path.join(script_dir, script_filename)
  108.     script_file = open(script_name, 'w')
  109.     script_file.write(source)
  110.     script_file.close()
  111.     return script_name
  112.  
  113.  
  114. def compile_script(script_name):
  115.     py_compile.compile(script_name, doraise = True)
  116.     compiled_name = script_name + 'c'
  117.     return compiled_name
  118.  
  119.  
  120. def make_zip_script(zip_dir, zip_basename, script_name, name_in_zip = None):
  121.     zip_filename = zip_basename + os.extsep + 'zip'
  122.     zip_name = os.path.join(zip_dir, zip_filename)
  123.     zip_file = zipfile.ZipFile(zip_name, 'w')
  124.     if name_in_zip is None:
  125.         name_in_zip = os.path.basename(script_name)
  126.     zip_file.write(script_name, name_in_zip)
  127.     zip_file.close()
  128.     return (zip_name, os.path.join(zip_name, name_in_zip))
  129.  
  130.  
  131. def make_pkg(pkg_dir):
  132.     os.mkdir(pkg_dir)
  133.     make_script(pkg_dir, '__init__', '')
  134.  
  135.  
  136. def make_zip_pkg(zip_dir, zip_basename, pkg_name, script_basename, source, depth = 1, compiled = False):
  137.     unlink = []
  138.     init_name = make_script(zip_dir, '__init__', '')
  139.     unlink.append(init_name)
  140.     init_basename = os.path.basename(init_name)
  141.     script_name = make_script(zip_dir, script_basename, source)
  142.     unlink.append(script_name)
  143.     if compiled:
  144.         init_name = compile_script(init_name)
  145.         script_name = compile_script(script_name)
  146.         unlink.extend((init_name, script_name))
  147.     pkg_names = [ os.sep.join([
  148.         pkg_name] * i) for i in range(1, depth + 1) ]
  149.     script_name_in_zip = os.path.join(pkg_names[-1], os.path.basename(script_name))
  150.     zip_filename = zip_basename + os.extsep + 'zip'
  151.     zip_name = os.path.join(zip_dir, zip_filename)
  152.     zip_file = zipfile.ZipFile(zip_name, 'w')
  153.     for name in pkg_names:
  154.         init_name_in_zip = os.path.join(name, init_basename)
  155.         zip_file.write(init_name, init_name_in_zip)
  156.     
  157.     zip_file.write(script_name, script_name_in_zip)
  158.     zip_file.close()
  159.     for name in unlink:
  160.         os.unlink(name)
  161.     
  162.     return (zip_name, os.path.join(zip_name, script_name_in_zip))
  163.  
  164.